3 * Special page which uses a ChangesList to show query results.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
23 use MediaWiki\Logger\LoggerFactory
;
24 use Wikimedia\Rdbms\ResultWrapper
;
25 use Wikimedia\Rdbms\IDatabase
;
28 * Special page which uses a ChangesList to show query results.
29 * @todo Way too many public functions, most of them should be protected
31 * @ingroup SpecialPage
33 abstract class ChangesListSpecialPage
extends SpecialPage
{
37 /** @var FormOptions */
41 protected $customFilters;
43 // Order of both groups and filters is significant; first is top-most priority,
44 // descending from there.
45 // 'showHideSuffix' is a shortcut to and avoid spelling out
46 // details specific to subclasses here.
48 * Definition information for the filters and their groups
50 * The value is $groupDefinition, a parameter to the ChangesListFilterGroup constructor.
51 * However, priority is dynamically added for the core groups, to ease maintenance.
53 * Groups are displayed to the user in the structured UI. However, if necessary,
54 * all of the filters in a group can be configured to only display on the
55 * unstuctured UI, in which case you don't need a group title. This is done in
56 * getFilterGroupDefinitionFromLegacyCustomFilters, for example.
58 * @var array $filterGroupDefinitions
60 private $filterGroupDefinitions;
62 // Same format as filterGroupDefinitions, but for a single group (reviewStatus)
63 // that is registered conditionally.
64 private $reviewStatusFilterGroupDefinition;
66 // Single filter registered conditionally
67 private $hideCategorizationFilterDefinition;
70 * Filter groups, and their contained filters
71 * This is an associative array (with group name as key) of ChangesListFilterGroup objects.
73 * @var array $filterGroups
75 protected $filterGroups = [];
77 public function __construct( $name, $restriction ) {
78 parent
::__construct( $name, $restriction );
80 $this->filterGroupDefinitions
= [
82 'name' => 'registration',
83 'title' => 'rcfilters-filtergroup-registration',
84 'class' => ChangesListBooleanFilterGroup
::class,
88 'label' => 'rcfilters-filter-registered-label',
89 'description' => 'rcfilters-filter-registered-description',
90 // rcshowhideliu-show, rcshowhideliu-hide,
92 'showHideSuffix' => 'showhideliu',
94 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
95 &$query_options, &$join_conds ) {
97 $conds[] = 'rc_user = 0';
99 'cssClassSuffix' => 'liu',
100 'isRowApplicableCallable' => function ( $ctx, $rc ) {
101 return $rc->getAttribute( 'rc_user' );
106 'name' => 'hideanons',
107 'label' => 'rcfilters-filter-unregistered-label',
108 'description' => 'rcfilters-filter-unregistered-description',
109 // rcshowhideanons-show, rcshowhideanons-hide,
111 'showHideSuffix' => 'showhideanons',
113 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
114 &$query_options, &$join_conds ) {
116 $conds[] = 'rc_user != 0';
118 'cssClassSuffix' => 'anon',
119 'isRowApplicableCallable' => function ( $ctx, $rc ) {
120 return !$rc->getAttribute( 'rc_user' );
127 'name' => 'userExpLevel',
128 'title' => 'rcfilters-filtergroup-userExpLevel',
129 'class' => ChangesListStringOptionsFilterGroup
::class,
130 // Excludes unregistered users
131 'isFullCoverage' => false,
134 'name' => 'newcomer',
135 'label' => 'rcfilters-filter-user-experience-level-newcomer-label',
136 'description' => 'rcfilters-filter-user-experience-level-newcomer-description',
137 'cssClassSuffix' => 'user-newcomer',
138 'isRowApplicableCallable' => function ( $ctx, $rc ) {
139 $performer = $rc->getPerformer();
140 return $performer && $performer->isLoggedIn() &&
141 $performer->getExperienceLevel() === 'newcomer';
146 'label' => 'rcfilters-filter-user-experience-level-learner-label',
147 'description' => 'rcfilters-filter-user-experience-level-learner-description',
148 'cssClassSuffix' => 'user-learner',
149 'isRowApplicableCallable' => function ( $ctx, $rc ) {
150 $performer = $rc->getPerformer();
151 return $performer && $performer->isLoggedIn() &&
152 $performer->getExperienceLevel() === 'learner';
156 'name' => 'experienced',
157 'label' => 'rcfilters-filter-user-experience-level-experienced-label',
158 'description' => 'rcfilters-filter-user-experience-level-experienced-description',
159 'cssClassSuffix' => 'user-experienced',
160 'isRowApplicableCallable' => function ( $ctx, $rc ) {
161 $performer = $rc->getPerformer();
162 return $performer && $performer->isLoggedIn() &&
163 $performer->getExperienceLevel() === 'experienced';
167 'default' => ChangesListStringOptionsFilterGroup
::NONE
,
168 'queryCallable' => [ $this, 'filterOnUserExperienceLevel' ],
172 'name' => 'authorship',
173 'title' => 'rcfilters-filtergroup-authorship',
174 'class' => ChangesListBooleanFilterGroup
::class,
177 'name' => 'hidemyself',
178 'label' => 'rcfilters-filter-editsbyself-label',
179 'description' => 'rcfilters-filter-editsbyself-description',
180 // rcshowhidemine-show, rcshowhidemine-hide,
182 'showHideSuffix' => 'showhidemine',
184 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
185 &$query_options, &$join_conds ) {
187 $user = $ctx->getUser();
188 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
190 'cssClassSuffix' => 'self',
191 'isRowApplicableCallable' => function ( $ctx, $rc ) {
192 return $ctx->getUser()->equals( $rc->getPerformer() );
196 'name' => 'hidebyothers',
197 'label' => 'rcfilters-filter-editsbyother-label',
198 'description' => 'rcfilters-filter-editsbyother-description',
200 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
201 &$query_options, &$join_conds ) {
203 $user = $ctx->getUser();
204 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
206 'cssClassSuffix' => 'others',
207 'isRowApplicableCallable' => function ( $ctx, $rc ) {
208 return !$ctx->getUser()->equals( $rc->getPerformer() );
215 'name' => 'automated',
216 'title' => 'rcfilters-filtergroup-automated',
217 'class' => ChangesListBooleanFilterGroup
::class,
220 'name' => 'hidebots',
221 'label' => 'rcfilters-filter-bots-label',
222 'description' => 'rcfilters-filter-bots-description',
223 // rcshowhidebots-show, rcshowhidebots-hide,
225 'showHideSuffix' => 'showhidebots',
227 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
228 &$query_options, &$join_conds ) {
230 $conds[] = 'rc_bot = 0';
232 'cssClassSuffix' => 'bot',
233 'isRowApplicableCallable' => function ( $ctx, $rc ) {
234 return $rc->getAttribute( 'rc_bot' );
238 'name' => 'hidehumans',
239 'label' => 'rcfilters-filter-humans-label',
240 'description' => 'rcfilters-filter-humans-description',
242 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
243 &$query_options, &$join_conds ) {
245 $conds[] = 'rc_bot = 1';
247 'cssClassSuffix' => 'human',
248 'isRowApplicableCallable' => function ( $ctx, $rc ) {
249 return !$rc->getAttribute( 'rc_bot' );
255 // reviewStatus (conditional)
258 'name' => 'significance',
259 'title' => 'rcfilters-filtergroup-significance',
260 'class' => ChangesListBooleanFilterGroup
::class,
264 'name' => 'hideminor',
265 'label' => 'rcfilters-filter-minor-label',
266 'description' => 'rcfilters-filter-minor-description',
267 // rcshowhideminor-show, rcshowhideminor-hide,
269 'showHideSuffix' => 'showhideminor',
271 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
272 &$query_options, &$join_conds ) {
274 $conds[] = 'rc_minor = 0';
276 'cssClassSuffix' => 'minor',
277 'isRowApplicableCallable' => function ( $ctx, $rc ) {
278 return $rc->getAttribute( 'rc_minor' );
282 'name' => 'hidemajor',
283 'label' => 'rcfilters-filter-major-label',
284 'description' => 'rcfilters-filter-major-description',
286 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
287 &$query_options, &$join_conds ) {
289 $conds[] = 'rc_minor = 1';
291 'cssClassSuffix' => 'major',
292 'isRowApplicableCallable' => function ( $ctx, $rc ) {
293 return !$rc->getAttribute( 'rc_minor' );
300 'name' => 'lastRevision',
301 'title' => 'rcfilters-filtergroup-lastRevision',
302 'class' => ChangesListBooleanFilterGroup
::class,
306 'name' => 'hidelastrevision',
307 'label' => 'rcfilters-filter-lastrevision-label',
308 'description' => 'rcfilters-filter-lastrevision-description',
310 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
311 &$query_options, &$join_conds ) {
312 $conds[] = 'rc_this_oldid <> page_latest';
314 'cssClassSuffix' => 'last',
315 'isRowApplicableCallable' => function ( $ctx, $rc ) {
316 return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
320 'name' => 'hidepreviousrevisions',
321 'label' => 'rcfilters-filter-previousrevision-label',
322 'description' => 'rcfilters-filter-previousrevision-description',
324 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
325 &$query_options, &$join_conds ) {
326 $conds[] = 'rc_this_oldid = page_latest';
328 'cssClassSuffix' => 'previous',
329 'isRowApplicableCallable' => function ( $ctx, $rc ) {
330 return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
336 // With extensions, there can be change types that will not be hidden by any of these.
338 'name' => 'changeType',
339 'title' => 'rcfilters-filtergroup-changetype',
340 'class' => ChangesListBooleanFilterGroup
::class,
344 'name' => 'hidepageedits',
345 'label' => 'rcfilters-filter-pageedits-label',
346 'description' => 'rcfilters-filter-pageedits-description',
349 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
350 &$query_options, &$join_conds ) {
352 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT
);
354 'cssClassSuffix' => 'src-mw-edit',
355 'isRowApplicableCallable' => function ( $ctx, $rc ) {
356 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_EDIT
;
360 'name' => 'hidenewpages',
361 'label' => 'rcfilters-filter-newpages-label',
362 'description' => 'rcfilters-filter-newpages-description',
365 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
366 &$query_options, &$join_conds ) {
368 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW
);
370 'cssClassSuffix' => 'src-mw-new',
371 'isRowApplicableCallable' => function ( $ctx, $rc ) {
372 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_NEW
;
376 // hidecategorization
380 'label' => 'rcfilters-filter-logactions-label',
381 'description' => 'rcfilters-filter-logactions-description',
384 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
385 &$query_options, &$join_conds ) {
387 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG
);
389 'cssClassSuffix' => 'src-mw-log',
390 'isRowApplicableCallable' => function ( $ctx, $rc ) {
391 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_LOG
;
399 $this->reviewStatusFilterGroupDefinition
= [
401 'name' => 'reviewStatus',
402 'title' => 'rcfilters-filtergroup-reviewstatus',
403 'class' => ChangesListBooleanFilterGroup
::class,
407 'name' => 'hidepatrolled',
408 'label' => 'rcfilters-filter-patrolled-label',
409 'description' => 'rcfilters-filter-patrolled-description',
410 // rcshowhidepatr-show, rcshowhidepatr-hide
412 'showHideSuffix' => 'showhidepatr',
414 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
415 &$query_options, &$join_conds ) {
417 $conds[] = 'rc_patrolled = 0';
419 'cssClassSuffix' => 'patrolled',
420 'isRowApplicableCallable' => function ( $ctx, $rc ) {
421 return $rc->getAttribute( 'rc_patrolled' );
425 'name' => 'hideunpatrolled',
426 'label' => 'rcfilters-filter-unpatrolled-label',
427 'description' => 'rcfilters-filter-unpatrolled-description',
429 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
430 &$query_options, &$join_conds ) {
432 $conds[] = 'rc_patrolled = 1';
434 'cssClassSuffix' => 'unpatrolled',
435 'isRowApplicableCallable' => function ( $ctx, $rc ) {
436 return !$rc->getAttribute( 'rc_patrolled' );
443 $this->hideCategorizationFilterDefinition
= [
444 'name' => 'hidecategorization',
445 'label' => 'rcfilters-filter-categorization-label',
446 'description' => 'rcfilters-filter-categorization-description',
447 // rcshowhidecategorization-show, rcshowhidecategorization-hide.
448 // wlshowhidecategorization
449 'showHideSuffix' => 'showhidecategorization',
452 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
453 &$query_options, &$join_conds ) {
455 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE
);
457 'cssClassSuffix' => 'src-mw-categorize',
458 'isRowApplicableCallable' => function ( $ctx, $rc ) {
459 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_CATEGORIZE
;
465 * Check if filters are in conflict and guaranteed to return no results.
469 protected function areFiltersInConflict() {
470 $opts = $this->getOptions();
471 /** @var ChangesListFilterGroup $group */
472 foreach ( $this->getFilterGroups() as $group ) {
474 if ( $group->getConflictingGroups() ) {
477 " specifies conflicts with other groups but these are not supported yet."
481 /** @var ChangesListFilter $conflictingFilter */
482 foreach ( $group->getConflictingFilters() as $conflictingFilter ) {
483 if ( $conflictingFilter->activelyInConflictWithGroup( $group, $opts ) ) {
488 /** @var ChangesListFilter $filter */
489 foreach ( $group->getFilters() as $filter ) {
491 /** @var ChangesListFilter $conflictingFilter */
492 foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
494 $conflictingFilter->activelyInConflictWithFilter( $filter, $opts ) &&
495 $filter->activelyInConflictWithFilter( $conflictingFilter, $opts )
509 * Main execution point
511 * @param string $subpage
513 public function execute( $subpage ) {
514 $this->rcSubpage
= $subpage;
517 $this->outputHeader();
520 $rows = $this->getRows();
521 $opts = $this->getOptions();
522 if ( $rows === false ) {
523 if ( !$this->including() ) {
524 $this->doHeader( $opts, 0 );
525 $this->outputNoResults();
526 $this->getOutput()->setStatusCode( 404 );
532 $batch = new LinkBatch
;
533 foreach ( $rows as $row ) {
534 $batch->add( NS_USER
, $row->rc_user_text
);
535 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
536 $batch->add( $row->rc_namespace
, $row->rc_title
);
537 if ( $row->rc_source
=== RecentChange
::SRC_LOG
) {
538 $formatter = LogFormatter
::newFromRow( $row );
539 foreach ( $formatter->getPreloadTitles() as $title ) {
540 $batch->addObj( $title );
545 $this->webOutput( $rows, $opts );
549 if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
550 // Clean up any bad page entries for titles showing up in RC
551 DeferredUpdates
::addUpdate( new WANCacheReapUpdate(
553 LoggerFactory
::getInstance( 'objectcache' )
559 * Add the "no results" message to the output
561 protected function outputNoResults() {
562 $this->getOutput()->addHTML(
563 '<div class="mw-changeslist-empty">' .
564 $this->msg( 'recentchanges-noresult' )->parse() .
570 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
572 * @return bool|ResultWrapper Result or false
574 public function getRows() {
575 $opts = $this->getOptions();
582 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
584 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
588 * Get the current FormOptions for this request
590 * @return FormOptions
592 public function getOptions() {
593 if ( $this->rcOptions
=== null ) {
594 $this->rcOptions
= $this->setup( $this->rcSubpage
);
597 return $this->rcOptions
;
601 * Register all filters and their groups (including those from hooks), plus handle
602 * conflicts and defaults.
604 * You might want to customize these in the same method, in subclasses. You can
605 * call getFilterGroup to access a group, and (on the group) getFilter to access a
606 * filter, then make necessary modfications to the filter or group (e.g. with
609 protected function registerFilters() {
610 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions
);
612 // Make sure this is not being transcluded (we don't want to show this
613 // information to all users just because the user that saves the edit can
614 // patrol or is logged in)
615 if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
616 $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition
);
619 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
621 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
622 $transformedHideCategorizationDef = $this->transformFilterDefinition(
623 $this->hideCategorizationFilterDefinition
626 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
628 $hideCategorization = new ChangesListBooleanFilter(
629 $transformedHideCategorizationDef
633 Hooks
::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
635 $unstructuredGroupDefinition =
636 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
637 $this->getCustomFilters()
639 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
641 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
643 $registration = $this->getFilterGroup( 'registration' );
644 $anons = $registration->getFilter( 'hideanons' );
646 // This means there is a conflict between any item in user experience level
647 // being checked and only anons being *shown* (hideliu=1&hideanons=0 in the
648 // URL, or equivalent).
649 $userExperienceLevel->conflictsWith(
651 'rcfilters-filtergroup-user-experience-level-conflicts-unregistered-global',
652 'rcfilters-filtergroup-user-experience-level-conflicts-unregistered',
653 'rcfilters-filter-unregistered-conflicts-user-experience-level'
656 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
657 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
658 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
660 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
661 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
663 // categoryFilter is conditional; see registerFilters
664 if ( $categoryFilter !== null ) {
665 $hideMinorFilter->conflictsWith(
667 'rcfilters-hideminor-conflicts-typeofchange-global',
668 'rcfilters-hideminor-conflicts-typeofchange',
669 'rcfilters-typeofchange-conflicts-hideminor'
672 $hideMinorFilter->conflictsWith(
674 'rcfilters-hideminor-conflicts-typeofchange-global',
675 'rcfilters-hideminor-conflicts-typeofchange',
676 'rcfilters-typeofchange-conflicts-hideminor'
678 $hideMinorFilter->conflictsWith(
680 'rcfilters-hideminor-conflicts-typeofchange-global',
681 'rcfilters-hideminor-conflicts-typeofchange',
682 'rcfilters-typeofchange-conflicts-hideminor'
687 * Transforms filter definition to prepare it for constructor.
689 * See overrides of this method as well.
691 * @param array $filterDefinition Original filter definition
693 * @return array Transformed definition
695 protected function transformFilterDefinition( array $filterDefinition ) {
696 return $filterDefinition;
700 * Register filters from a definition object
702 * Array specifying groups and their filters; see Filter and
703 * ChangesListFilterGroup constructors.
705 * There is light processing to simplify core maintenance.
707 protected function registerFiltersFromDefinitions( array $definition ) {
708 $autoFillPriority = -1;
709 foreach ( $definition as $groupDefinition ) {
710 if ( !isset( $groupDefinition['priority'] ) ) {
711 $groupDefinition['priority'] = $autoFillPriority;
713 // If it's explicitly specified, start over the auto-fill
714 $autoFillPriority = $groupDefinition['priority'];
719 $className = $groupDefinition['class'];
720 unset( $groupDefinition['class'] );
722 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
723 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
726 $this->registerFilterGroup( new $className( $groupDefinition ) );
731 * Get filter group definition from legacy custom filters
733 * @param array $customFilters Custom filters from legacy hooks
734 * @return array Group definition
736 protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
737 // Special internal unstructured group
738 $unstructuredGroupDefinition = [
739 'name' => 'unstructured',
740 'class' => ChangesListBooleanFilterGroup
::class,
741 'priority' => -1, // Won't display in structured
745 foreach ( $customFilters as $name => $params ) {
746 $unstructuredGroupDefinition['filters'][] = [
748 'showHide' => $params['msg'],
749 'default' => $params['default'],
753 return $unstructuredGroupDefinition;
757 * Register all the filters, including legacy hook-driven ones.
758 * Then create a FormOptions object with options as specified by the user
760 * @param array $parameters
762 * @return FormOptions
764 public function setup( $parameters ) {
765 $this->registerFilters();
767 $opts = $this->getDefaultOptions();
769 $opts = $this->fetchOptionsFromRequest( $opts );
771 // Give precedence to subpage syntax
772 if ( $parameters !== null ) {
773 $this->parseParameters( $parameters, $opts );
776 $this->validateOptions( $opts );
782 * Get a FormOptions object containing the default options. By default, returns
783 * some basic options. The filters listed explicitly here are overriden in this
784 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
785 * and more) are structured. Structured filters are overriden in registerFilters.
788 * @return FormOptions
790 public function getDefaultOptions() {
791 $config = $this->getConfig();
792 $opts = new FormOptions();
793 $structuredUI = $this->getUser()->getOption( 'rcenhancedfilters' );
796 foreach ( $this->filterGroups
as $filterGroup ) {
797 // URL parameters can be per-group, like 'userExpLevel',
798 // or per-filter, like 'hideminor'.
799 if ( $filterGroup->isPerGroupRequestParameter() ) {
800 $opts->add( $filterGroup->getName(), $filterGroup->getDefault() );
802 foreach ( $filterGroup->getFilters() as $filter ) {
803 $opts->add( $filter->getName(), $filter->getDefault( $structuredUI ) );
808 $opts->add( 'namespace', '', FormOptions
::STRING );
809 $opts->add( 'invert', false );
810 $opts->add( 'associated', false );
816 * Register a structured changes list filter group
818 * @param ChangesListFilterGroup $group
820 public function registerFilterGroup( ChangesListFilterGroup
$group ) {
821 $groupName = $group->getName();
823 $this->filterGroups
[$groupName] = $group;
827 * Gets the currently registered filters groups
829 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
831 protected function getFilterGroups() {
832 return $this->filterGroups
;
836 * Gets a specified ChangesListFilterGroup by name
838 * @param string $groupName Name of group
840 * @return ChangesListFilterGroup|null Group, or null if not registered
842 public function getFilterGroup( $groupName ) {
843 return isset( $this->filterGroups
[$groupName] ) ?
844 $this->filterGroups
[$groupName] :
848 // Currently, this intentionally only includes filters that display
849 // in the structured UI. This can be changed easily, though, if we want
850 // to include data on filters that use the unstructured UI. messageKeys is a
851 // special top-level value, with the value being an array of the message keys to
852 // send to the client.
854 * Gets structured filter information needed by JS
856 * @return array Associative array
857 * * array $return['groups'] Group data
858 * * array $return['messageKeys'] Array of message keys
860 public function getStructuredFilterJsData() {
866 $context = $this->getContext();
868 usort( $this->filterGroups
, function ( $a, $b ) {
869 return $b->getPriority() - $a->getPriority();
872 foreach ( $this->filterGroups
as $groupName => $group ) {
873 $groupOutput = $group->getJsData( $this );
874 if ( $groupOutput !== null ) {
875 $output['messageKeys'] = array_merge(
876 $output['messageKeys'],
877 $groupOutput['messageKeys']
880 unset( $groupOutput['messageKeys'] );
881 $output['groups'][] = $groupOutput;
889 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
892 * @return array Map of filter URL param names to properties (msg/default)
894 protected function getCustomFilters() {
895 if ( $this->customFilters
=== null ) {
896 $this->customFilters
= [];
897 Hooks
::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters
], '1.29' );
900 return $this->customFilters
;
904 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
906 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
908 * @param FormOptions $opts
909 * @return FormOptions
911 protected function fetchOptionsFromRequest( $opts ) {
912 $opts->fetchValuesFromRequest( $this->getRequest() );
918 * Process $par and put options found in $opts. Used when including the page.
921 * @param FormOptions $opts
923 public function parseParameters( $par, FormOptions
$opts ) {
924 $stringParameterNameSet = [];
925 $hideParameterNameSet = [];
927 // URL parameters can be per-group, like 'userExpLevel',
928 // or per-filter, like 'hideminor'.
930 foreach ( $this->filterGroups
as $filterGroup ) {
931 if ( $filterGroup->isPerGroupRequestParameter() ) {
932 $stringParameterNameSet[$filterGroup->getName()] = true;
933 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup
::TYPE
) {
934 foreach ( $filterGroup->getFilters() as $filter ) {
935 $hideParameterNameSet[$filter->getName()] = true;
940 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
941 foreach ( $bits as $bit ) {
943 if ( isset( $hideParameterNameSet[$bit] ) ) {
944 // hidefoo => hidefoo=true
946 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
947 // foo => hidefoo=false
948 $opts["hide$bit"] = false;
949 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
950 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
951 $opts[$m[1]] = $m[2];
958 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
960 * @param FormOptions $opts
962 public function validateOptions( FormOptions
$opts ) {
963 if ( $this->fixContradictoryOptions( $opts ) ) {
964 $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
965 $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
970 * Fix invalid options by resetting pairs that should never appear together.
972 * @param FormOptions $opts
973 * @return bool True if any option was reset
975 private function fixContradictoryOptions( FormOptions
$opts ) {
976 $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
978 foreach ( $this->filterGroups
as $filterGroup ) {
979 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
980 $filters = $filterGroup->getFilters();
982 if ( count( $filters ) === 1 ) {
983 // legacy boolean filters should not be considered
987 $allInGroupEnabled = array_reduce(
989 function ( $carry, $filter ) use ( $opts ) {
990 return $carry && $opts[ $filter->getName() ];
992 /* initialValue */ count( $filters ) > 0
995 if ( $allInGroupEnabled ) {
996 foreach ( $filters as $filter ) {
997 $opts[ $filter->getName() ] = false;
1009 * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
1012 * This is deprecated and may be removed.
1014 * @param FormOptions $opts
1015 * @return bool True if this change was mode
1017 private function fixBackwardsCompatibilityOptions( FormOptions
$opts ) {
1018 if ( $opts['hideanons'] && $opts['hideliu'] ) {
1019 $opts->reset( 'hideanons' );
1020 if ( !$opts['hidebots'] ) {
1021 $opts->reset( 'hideliu' );
1022 $opts['hidehumans'] = 1;
1032 * Convert parameters values from true/false to 1/0
1033 * so they are not omitted by wfArrayToCgi()
1036 * @param array $params
1039 protected function convertParamsForLink( $params ) {
1040 foreach ( $params as &$value ) {
1041 if ( $value === false ) {
1050 * Sets appropriate tables, fields, conditions, etc. depending on which filters
1051 * the user requested.
1053 * @param array &$tables Array of tables; see IDatabase::select $table
1054 * @param array &$fields Array of fields; see IDatabase::select $vars
1055 * @param array &$conds Array of conditions; see IDatabase::select $conds
1056 * @param array &$query_options Array of query options; see IDatabase::select $options
1057 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1058 * @param FormOptions $opts
1060 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
1061 &$join_conds, FormOptions
$opts ) {
1063 $dbr = $this->getDB();
1064 $user = $this->getUser();
1066 $context = $this->getContext();
1067 foreach ( $this->filterGroups
as $filterGroup ) {
1068 // URL parameters can be per-group, like 'userExpLevel',
1069 // or per-filter, like 'hideminor'.
1070 if ( $filterGroup->isPerGroupRequestParameter() ) {
1071 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1072 $query_options, $join_conds, $opts[$filterGroup->getName()] );
1074 foreach ( $filterGroup->getFilters() as $filter ) {
1075 if ( $opts[$filter->getName()] ) {
1076 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1077 $query_options, $join_conds );
1083 // Namespace filtering
1084 if ( $opts[ 'namespace' ] !== '' ) {
1085 $namespaces = explode( ';', $opts[ 'namespace' ] );
1087 if ( $opts[ 'associated' ] ) {
1088 $associatedNamespaces = array_map(
1090 return MWNamespace
::getAssociated( $ns );
1094 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1097 if ( count( $namespaces ) === 1 ) {
1098 $operator = $opts[ 'invert' ] ?
'!=' : '=';
1099 $value = $dbr->addQuotes( reset( $namespaces ) );
1101 $operator = $opts[ 'invert' ] ?
'NOT IN' : 'IN';
1102 sort( $namespaces );
1103 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1105 $conds[] = "rc_namespace $operator $value";
1112 * @param array $tables Array of tables; see IDatabase::select $table
1113 * @param array $fields Array of fields; see IDatabase::select $vars
1114 * @param array $conds Array of conditions; see IDatabase::select $conds
1115 * @param array $query_options Array of query options; see IDatabase::select $options
1116 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1117 * @param FormOptions $opts
1118 * @return bool|ResultWrapper Result or false
1120 protected function doMainQuery( $tables, $fields, $conds,
1121 $query_options, $join_conds, FormOptions
$opts ) {
1123 $tables[] = 'recentchanges';
1124 $fields = array_merge( RecentChange
::selectFields(), $fields );
1126 ChangeTags
::modifyDisplayQuery(
1135 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1141 $dbr = $this->getDB();
1143 return $dbr->select(
1153 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1154 &$query_options, &$join_conds, $opts
1157 'ChangesListSpecialPageQuery',
1158 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1163 * Return a IDatabase object for reading
1167 protected function getDB() {
1168 return wfGetDB( DB_REPLICA
);
1172 * Send output to the OutputPage object, only called if not used feeds
1174 * @param ResultWrapper $rows Database rows
1175 * @param FormOptions $opts
1177 public function webOutput( $rows, $opts ) {
1178 if ( !$this->including() ) {
1179 $this->outputFeedLinks();
1180 $this->doHeader( $opts, $rows->numRows() );
1183 $this->outputChangesList( $rows, $opts );
1187 * Output feed links.
1189 public function outputFeedLinks() {
1190 // nothing by default
1194 * Build and output the actual changes list.
1196 * @param ResultWrapper $rows Database rows
1197 * @param FormOptions $opts
1199 abstract public function outputChangesList( $rows, $opts );
1202 * Set the text to be displayed above the changes
1204 * @param FormOptions $opts
1205 * @param int $numRows Number of rows in the result to show after this header
1207 public function doHeader( $opts, $numRows ) {
1208 $this->setTopText( $opts );
1210 // @todo Lots of stuff should be done here.
1212 $this->setBottomText( $opts );
1216 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1217 * or similar methods to print the text.
1219 * @param FormOptions $opts
1221 public function setTopText( FormOptions
$opts ) {
1222 // nothing by default
1226 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1227 * or similar methods to print the text.
1229 * @param FormOptions $opts
1231 public function setBottomText( FormOptions
$opts ) {
1232 // nothing by default
1236 * Get options to be displayed in a form
1237 * @todo This should handle options returned by getDefaultOptions().
1238 * @todo Not called by anything in this class (but is in subclasses), should be
1239 * called by something… doHeader() maybe?
1241 * @param FormOptions $opts
1244 public function getExtraOptions( $opts ) {
1249 * Return the legend displayed within the fieldset
1253 public function makeLegend() {
1254 $context = $this->getContext();
1255 $user = $context->getUser();
1256 # The legend showing what the letters and stuff mean
1257 $legend = Html
::openElement( 'dl' ) . "\n";
1258 # Iterates through them and gets the messages for both letter and tooltip
1259 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1260 if ( !( $user->useRCPatrol() ||
$user->useNPPatrol() ) ) {
1261 unset( $legendItems['unpatrolled'] );
1263 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1264 $label = isset( $item['legend'] ) ?
$item['legend'] : $item['title'];
1265 $letter = $item['letter'];
1266 $cssClass = isset( $item['class'] ) ?
$item['class'] : $key;
1268 $legend .= Html
::element( 'dt',
1269 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1271 Html
::rawElement( 'dd',
1272 [ 'class' => Sanitizer
::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1273 $context->msg( $label )->parse()
1277 $legend .= Html
::rawElement( 'dt',
1278 [ 'class' => 'mw-plusminus-pos' ],
1279 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1281 $legend .= Html
::element(
1283 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1284 $context->msg( 'recentchanges-label-plusminus' )->text()
1286 $legend .= Html
::closeElement( 'dl' ) . "\n";
1290 '<div class="mw-changeslist-legend">' .
1291 $context->msg( 'recentchanges-legend-heading' )->parse() .
1292 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1299 * Add page-specific modules.
1301 protected function addModules() {
1302 $out = $this->getOutput();
1303 // Styles and behavior for the legend box (see makeLegend())
1304 $out->addModuleStyles( [
1305 'mediawiki.special.changeslist.legend',
1306 'mediawiki.special.changeslist',
1308 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1311 protected function getGroupName() {
1316 * Filter on users' experience levels; this will not be called if nothing is
1319 * @param string $specialPageClassName Class name of current special page
1320 * @param IContextSource $context Context, for e.g. user
1321 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1322 * @param array &$tables Array of tables; see IDatabase::select $table
1323 * @param array &$fields Array of fields; see IDatabase::select $vars
1324 * @param array &$conds Array of conditions; see IDatabase::select $conds
1325 * @param array &$query_options Array of query options; see IDatabase::select $options
1326 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1327 * @param array $selectedExpLevels The allowed active values, sorted
1328 * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
1331 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1332 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0 ) {
1334 global $wgLearnerEdits,
1335 $wgExperiencedUserEdits,
1336 $wgLearnerMemberSince,
1337 $wgExperiencedUserMemberSince;
1341 // If all levels are selected, all logged-in users are included (but no
1342 // anons), so we can short-circuit.
1343 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1344 $conds[] = 'rc_user != 0';
1349 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1354 $secondsPerDay = 86400;
1355 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1356 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1358 $aboveNewcomer = $dbr->makeList(
1360 'user_editcount >= ' . intval( $wgLearnerEdits ),
1361 'user_registration <= ' . $dbr->timestamp( $learnerCutoff ),
1366 $aboveLearner = $dbr->makeList(
1368 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1369 'user_registration <= ' . $dbr->timestamp( $experiencedUserCutoff ),
1374 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1375 $conds[] = "NOT ( $aboveNewcomer )";
1376 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1377 $conds[] = $dbr->makeList(
1378 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1381 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1382 $conds[] = $aboveLearner;
1383 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1384 $conds[] = "NOT ( $aboveLearner )";
1385 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1386 $conds[] = $dbr->makeList(
1387 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1390 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1391 $conds[] = $aboveNewcomer;